#include "tcp.h"

//获取server ip prot
void get_local_ip(int sockfd,char * ip,unsigned short * port){
  
}

//tcp初始化
int tcp_init(const char * ip, unsigned short port){
    debug_log("tcp_init");
    int listenfd = socket(AF_INET,SOCK_STREAM,0);
    printf("%d\n",listenfd);
    if(-1 == listenfd)
        error_log("tcp_init::socket");
    //设置网络地址
    struct sockaddr_in serveraddr;
    memset(&serveraddr,0,sizeof(struct sockaddr_in));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(ip);
    serveraddr.sin_port = htons(port);
    //重复使用端口
    int on =1;
    int ret = setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
    
    if(-1 == ret)
        error_log("tcp_init::setsockopt");
    //绑定网络地址
    ret = bind(listenfd,(struct sockaddr *) &serveraddr,sizeof(serveraddr));
    if(-1 == ret)
        error_log("tcp_init::bind");
    ret = listen(listenfd,SERVER_CONNECT_MAXNUM);
    if(-1 == ret)
        error_log("tcp_init::listen");
    return listenfd;
}